package com.example.util;

import com.example.pojo.OldPhoto;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Qiniu {
    private static  final String accessKey = "5ILmBDFoaiMpbZ9aaH0okwNUu4pIuYKuY-h4qyUk";
    private static  final String secretKey = "EyUl1lQZJd4_-td5bHh_jReKhkIg5HXSMgEigpPo";
    private static  final String bucket = "noerror";
    private static final String domain="http://r4ekgx3jf.hn-bkt.clouddn.com";
    private  static final Auth auth = Auth.create(accessKey, secretKey);
    private static String  key = null;

    private static Configuration cfg = new Configuration(Region.region2());
    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(cfg);

    public static Object upload(String localFilePath){
        String  upToken = auth.uploadToken(bucket);
        DefaultPutRet putRet=null;
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
        }
        return domain+"\\"+putRet.hash;
    }

   /* public  static Object download(OldPhoto oldPhoto, MultipartFile image){
            File oldPhotoFolder=new File("E:\\骰子\\骰子\\src\\main\\resources\\static\\oldPhoto");
            File newFile=new File(oldPhotoFolder,oldPhoto.getPhotoId()+"jpg");
            if(!newFile.getParentFile().exists())
                newFile.getParentFile().mkdir();

            try {
            image.transferTo(newFile);


            } catch (IOException e) {
            e.printStackTrace();
        }
    return newFile.getAbsolutePath();
    }*/

  /*  public static void download(String targetUrl){
        String downloadUrl=auth.privateDownloadUrl(targetUrl);


    }*/
}
